package com.atguigu.admin.utils;


import cn.hutool.json.JSONObject;
import java.util.*;


/**
 * Created by shenjie on 2017/11/07.
 */
public final class DataUtil {
    private DataUtil() {
    }

    /**
     * 判断对象是否Empty(null或元素为0)<br>
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     *
     * @param pObj 待检查对象
     * @return boolean 返回的布尔值
     */
    public static final boolean isEmpty(Object pObj) {
        if (pObj == null)
            return true;
        if (pObj == "")
            return true;
        if (pObj instanceof String) {
            if (((String) pObj).trim().length() == 0) {
                return true;
            }
        } else if (pObj instanceof Collection<?>) {
            if (((Collection<?>) pObj).size() == 0) {
                return true;
            }
        } else if (pObj instanceof Map<?, ?>) {
            if (((Map<?, ?>) pObj).size() == 0) {
                return true;
            }
        } else if (pObj instanceof Object[]) {
            if (((Object[]) pObj).length == 0) {
                return true;
            }
        }
        return false;
    }

    public static final boolean isNull(Object pObj) {
        if (pObj == null)
            return true;
        if (pObj == "")
            return true;
        if (pObj instanceof String) {
            if (((String) pObj).trim().length() == 0) {
                return true;
            }
        }
        return false;
    }


    /**
     * 判断json对象是否为空
     *
     * @param jsonObject
     * @return
     */
    public static final boolean isJsonObjEmpty(JSONObject jsonObject) {
        if (DataUtil.isEmpty(jsonObject)) {
            return true;
        }

        return jsonObject.keySet().size() == 0;
    }

    /**
     * 判断对象是否为NotEmpty(!null或元素>0)<br>
     * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
     *
     * @param pObj 待检查对象
     * @return boolean 返回的布尔值
     */
    public static final boolean isNotEmpty(Object pObj) {
        if (pObj == null)
            return false;
        if (pObj == "")
            return false;
        if (pObj instanceof String) {
            if (((String) pObj).trim().length() == 0) {
                return false;
            }
        } else if (pObj instanceof Collection<?>) {
            if (((Collection<?>) pObj).size() == 0) {
                return false;
            }
        } else if (pObj instanceof Map<?, ?>) {
            if (((Map<?, ?>) pObj).size() == 0) {
                return false;
            }
        } else if (pObj instanceof Object[]) {
            if (((Object[]) pObj).length == 0) {
                return false;
            }
        }
        return true;
    }
}
